package com.isomentricengine.tile
{
	import com.medusa.core.extension.SpriteExtension;
	
	import flash.display.Graphics;
	import flash.display.Sprite;

	public class BaseTile extends SpriteExtension implements ITile
	{
		private var ladderClip:Sprite;
		
		protected var _id:Number;
		protected var _width:Number;
		protected var _height:Number;
		protected var _walkable:Boolean;
		protected var _isDoor:Boolean;
		protected var _doorType:String;
		protected var _column:Number;
		protected var _row:Number;
		protected var _isCloud:Boolean;
		protected var _isLadder:Boolean;
		protected var _data:Object;

		public function BaseTile(w:Number, h:Number)
		{
			_width = w;
			_height = h;
		}
		
		public function build():void
		{
			// for override
		}
		
		public function get id():Number
		{
			return _id;
		}

		public function set id(value:Number):void
		{
			_id = value;
		}
		
		override public function get width():Number
		{
			return _width;
		}

		override public function set width(value:Number):void
		{
			_width = value;
		}

		override public function get height():Number
		{
			return _height;
		}

		override public function set height(value:Number):void
		{
			_height = value;
		}

		public function get walkable():Boolean
		{
			return _walkable;
		}

		public function set walkable(value:Boolean):void
		{
			_walkable = value;
		}
		
		public function get isDoor():Boolean
		{
			return _isDoor;
		}

		public function set isDoor(value:Boolean):void
		{
			_isDoor = value;
		}
		
		public function get doorType():String
		{
			return _doorType;
		}

		public function set doorType(value:String):void
		{
			_doorType = value;
		}
		
		public function get column():Number
		{
			return _column;
		}

		public function set column(value:Number):void
		{
			_column = value;
		}

		public function get row():Number
		{
			return _row;
		}

		public function set row(value:Number):void
		{
			_row = value;
		}
		
		public function get isCloud():Boolean
		{
			return _isCloud;
		}

		public function set isCloud(value:Boolean):void
		{
			_isCloud = value;
		}
		
		public function get data():Object
		{
			return _data;
		}

		public function set data(value:Object):void
		{
			_data = value;
		}
		
		public function get isLadder():Boolean
		{
			return _isLadder;
		}

		public function set isLadder(value:Boolean):void
		{
			_isLadder = value;
			if(value){
				drawLadder();
			}else{
				removeLadder();
			}
		}
		
		private function drawLadder():void
		{
			ladderClip = new Sprite();
			
			var ladderWidth:Number = width * 0.33;
			var ladderHeight:Number = height;
			var ladderX:Number = ladderWidth;
			var g:Graphics = ladderClip.graphics;
			
			g.lineStyle(1.75, 0x997766);
			g.moveTo(0, 0)
			g.lineTo(0, ladderHeight);
			g.moveTo(ladderWidth, 0);
			g.lineTo(ladderWidth, ladderHeight);
			
			for(var a:Number = 0; a<6; a++){
				g.moveTo(0, (ladderHeight * 0.2) * a);
				g.lineTo(ladderWidth, (ladderHeight * 0.2) * a);
			}
			
			ladderClip.x = ladderX;
			addChild(ladderClip);
		}
		
		private function removeLadder():void
		{
			if(ladderClip != null)
			{
				removeChild(ladderClip);
				ladderClip = null;
			}
		}
		
	}
}